import 'particles.js';
import { TAB_MENU_LIST } from '../store/user';
interface Industry {
  code: string;
  name: string;
  children: Industry[];
  sfcCode: string;
  snCode: string;
  cvCode: string;
  fullValue: string;
  fullName: string;
}
export interface LocalParams {
  searchParams?: any;
  infoStatusList?: string[];
  qualityStatusList?: string[];
  pageParam: any;
  testStatus?: string[];
  infoStatus?: string[];
}
// 登录动画
export const particles = () => {
    particlesJS('particles-background', {
        particles: {
          number: {
            value: 30,
            density: {
              enable: true,
              value_area: 1000
            }
          },
          color: {
            value: '#bebebe'
          },
          shape: {
            type: 'circle',
            stroke: {
              width: 0,
              color: '#ffffff'
            },
            polygon: {
              nb_sides: 5
            },
            image: {
              src: 'img/github.svg',
              width: 100,
              height: 100
            }
          },
          opacity: {
            value: 0.3
          },
          size: {
            value: 15,
            random: true,
            anim: {
              enable: false,
              speed: 40,
              size_min: 0.1,
              sync: false
            }
          },
          line_linked: {
            enable: true,
            distance: 300,
            color: '#c5c5c5',
            opacity: 0.4,
            width: 1
          },
          move: {
            enable: true,
            speed: .5,
            direction: 'none',
            random: true,
            straight: false,
            out_mode: 'out',
            bounce: false,
            attract: {
              enable: false,
              rotateX: 600,
              rotateY: 1200
            }
          }
        },
        interactivity: {
          detect_on: 'canvas',
          events: {
            onhover: {
              enable: false,
              mode: 'repulse'
            },
            onclick: {
              enable: false,
              mode: 'push'
            },
            resize: true
          },
          modes: {
            grab: {
              distance: 400,
              line_linked: {
                opacity: 1
              }
            },
            bubble: {
              distance: 400,
              size: 40,
              duration: 2,
              opacity: 0.8471528471528471,
              speed: 3
            },
            repulse: {
              distance: 200,
              duration: 0.4
            },
            push: {
              particles_nb: 4
            },
            remove: {
              particles_nb: 2
            }
          }
        },
        retina_detect: true
      });
};
// 数组根据本数组中对象的某个key排序
export const sortByLevel = (x, y) => {
    if (x.level - y.level > 0 ) {
      return 1;
    } 
    if (x.level - y.level < 0 ) {
      return -1;
    }
    if (x.level - y.level === 0 ) {
      return 0;
    }
    return 0;
};
// 把字典转成 el-cascader 组件需要的格式
export const getCascaderData = ({ data = [], name = 'name', key = 'code', children = 'children' }) => {
  const newArr = data.map((x: any) => {
      const _arr: {value: string; label: string; children: object[]|null} = {
          value: x[key],
          label: x[name],
          children: null
      };
      if (x[children] && x[children].length !== 0) {
          _arr.children = getCascaderData({ data: x[children], name, key, children });
      } else {
          _arr.children = null;
      }
      return _arr;
  });
  return newArr;
};
// 根据行业id获取行业中对应的数据 id: 行业的code  data: 行业的列表  key: 需要显示的字段
export const getIndustryById = (id: string|null, data: Industry[], key: string) => {
  if (!id) {
    return '';
  }
  let name: string | null;
  function listTree(list: {code: string; fullValue: string; fullName: string}[]) {
      if (list.length === 0) {
          return name;
      }
      list.map((item: Industry) => {
          if (id === item.code) {
              name = item[key];
          } else if (item.children && item.children.length !== 0) {
              name = listTree(item.children);
          }
      });
      return name;
  }
  return listTree(data);
};
// 循环行业生成fullName 和fullValue
export const setFullValue = ( data: Industry[]) => {
  function listTree(list: Industry[]) {
      const newList = list.map((item: Industry) => {
        if (item.children && item.children.length !== 0) {
          item.children.map(o => {
            o.fullValue = item.code + '-' + o.code;
            o.fullName = item.name + '-' + o.name;
            if (o.children && o.children.length !== 0) {
              o.children.map(x => {
                x.fullValue = item.code + '-' + o.code + '-' + x.code;
                x.fullName = item.name + '-' + o.name + '-' + x.name;
                return x;
              });
            }
            return o;
          });
        }
        return item;
      });
      return newList;
  }
  return listTree(data);
};
// 获取列表查询参数 存入 local
export const setParamsForList = (params: LocalParams, name: string|undefined) => {
  const localTabList = localStorage.getItem(TAB_MENU_LIST);
  if (localTabList) {
    const tabOpenedList = JSON.parse(localTabList);
    const index = tabOpenedList.findIndex( x => x.url === name);
    if (index !== -1) {
        // tslint:disable-next-line:no-string-literal
        tabOpenedList[index]['params'] = params;
    }
    localStorage.setItem(TAB_MENU_LIST, JSON.stringify(tabOpenedList));
  }
};
export const getParamsForList = (name: string|undefined) => {
  const localTabList = localStorage.getItem(TAB_MENU_LIST);
  if (localTabList) {
    const tabOpenedList = JSON.parse(localTabList);
    const list = tabOpenedList.filter( x => x.url === name);
    if (list && list.length && list[0].params) {
      const params: LocalParams = list[0].params;
      return params;
    } else {
      return { searchParams: {}, pageParam: {} } as LocalParams;
    }
  } else {
    return { searchParams: {}, pageParam: {} } as LocalParams;
  }
};

export const getListByChildren = (data: any) => {
  const list = data.reduce((pre, curr) => {
    return pre.concat(curr, curr.children);
  }, []);
  return list;
};